package com.dict.classes;

import java.sql.ResultSet;
import java.util.HashMap;
import java.util.Vector;

public class Words extends Word{
	private int pos;
	public Vector WordsVec;

	public void load()  throws Exception 
	{		
		this.pos = -1;
		this.WordsVec = new Vector();
		WordDb db = new WordDb();		
		ResultSet rs = db.getAllWordsSet();
		
		while(rs.next())
		{ 
			Word jObject = new Word();
			jObject.setId(rs.getInt(1));
			jObject.setWord(rs.getString(2));
			jObject.setMeaning(rs.getString(3));
			jObject.setUserId(rs.getInt(4));
			this.WordsVec.add(jObject);
		}
		db.disconnect();
	}

	public int size()
	{
		if(this.isNull())
			return 0;
		else
			return this.WordsVec.size();
	}

	public boolean isNull()
	{
		if(this.WordsVec == null)
			return true;
		else
			return false;
	}

	public void add(Word anObject)
	{
		if(this.isNull())
		{
			this.pos = -1;
			this.WordsVec = new Vector();
		}
		this.WordsVec.add(anObject);
	}
	
	public boolean next()
	{
		if(!(this.isNull())) 
		{
			pos++;
			return  (pos < this.WordsVec.size());
		} 
		else
		{
			return false;
		}
	}

	public Word getNext() 
	{
		return (Word) this.WordsVec.elementAt(this.pos);
	}
	
	public HashMap<Integer, String> getUsernamesMap(int [] userIds) throws Exception
	{
		HashMap<Integer, String> userMap = new HashMap<Integer, String>();
		
		WordDb db = new WordDb();
		ResultSet rs = db.getUserNames(userIds);
		
		while(rs.next())
		{
			userMap.put(rs.getInt(1), rs.getString(2));
		}
		
		
		return userMap;
	}
	public void reset(){
		pos = -1;
	}
}
